<template>
  <common-dialog ref="dialog" width="500px" :inDialog="inDialog" @closed="onClosed" @close="onClose" :title="title">
    <common-form ref="form" :rules="rules" :data="data" @keyup.enter.native="save">
      <el-divider>用户信息</el-divider>
      <el-form-item label="登录名" prop="loginName">
        <el-input v-model="data.loginName" placeholder="填写用户登录名"></el-input>
      </el-form-item>
      <el-form-item label="修改密码" prop="change_pwd" v-if="data.id != 0">
        <el-switch v-model="data.changePassword" />
      </el-form-item>
      <el-form-item label="登录密码" prop="loginPassword" v-if="!data.id != 0 || data.changePassword">
        <el-input v-model="data.loginPassword" type="password" placeholder="用户登录密码"></el-input>
      </el-form-item>
      <el-form-item label="所属角色" prop="roleId">
        <role-select v-model="data.roleId" />
      </el-form-item>
      <el-form-item label="禁用" prop="isDisabled">
        <el-switch v-model="data.isDisabled"></el-switch>
      </el-form-item>
    </common-form>
    <template #footer>
      <el-button type="primary" @click="save" :loading="$refs.dialog && $refs.dialog.isLoading">保存</el-button>
    </template>
  </common-dialog>
</template>
<script>
import commonDialog from "@/components/common/dialog";
import commonForm from "@/components/common/form";
import roleSelect from "@/components/sys/role/common/select";
import api from "@/api/sys/user";

export default {
  components: { commonDialog, roleSelect, commonForm },
  props: {
    inDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      data: {
        id: 0,
        loginName: "",
        loginPassword: "",
        changePassword: false,
        roleId: null,
        isDisabled: false,
      },
      rules: {
        name: [{ required: true, message: "请填写用户名称" }],
        loginName: [{ required: true, message: "请填写登录名" }],
        loginPassword: [{ required: true, message: "请填写登录密码" }],
        roleId: [{ required: true, message: "请选择所属角色" }],
      },
      title: "",
      isSaved: false
    };
  },
  computed: {},
  methods: {
    async showCreate() {
      this.title = "创建用户"
      await this.$refs.dialog.show();
    },
    async showEdit(data) {
      this.title = "编辑用户"
      await this.$refs.dialog.show();
      this.$refs.form.setData(data);
    },
    async save() {
      this.$refs.dialog.loading();
      let valid = await this.$refs.form.validate();
      if (!valid) {
        this.$refs.dialog.loading(false);
        return;
      }
      this.$refs.dialog.loading();
      let res = await api.save(this.data);
      if (res.code != 0) {
        return;
      }
      this.$message.success("保存成功");
      this.data.id = res.data;
      this.isSaved = true;
      this.$refs.dialog.loading(false);
    },
    onClosed() {
      this.title = "";
      this.isSaved = false;
      this.$refs.form.reset();
    },
    onClose() {
      if (this.isSaved) {
        this.$emit("close-saved");
      }
    }
  }
};
</script>